//
//  AlbumViewController.m
//  LZMusicPlayer
//
//  Created by wulanzhou on 2016/11/8.
//  Copyright © 2016年 wulanzhou. All rights reserved.
//

#import "AlbumViewController.h"
#import "BMAAlbum.h"
#import "GeDanCollectionViewCell.h"
#import "AlbumRecmd.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface AlbumViewController () <UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong) NSArray *sourceData;
@property (nonatomic,strong) UICollectionView *albumCollectionView;
@end

@implementation AlbumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"专辑";
    
    [self.view addSubview:self.albumCollectionView];
    
    BMAAlbum *request = [[BMAAlbum alloc] init];
    __weak BMAAlbum *WSELF = request;
    [request setCZRequestURL:^NSString *{
        return [WSELF recommendAlbumOffset:0 limmit:30];
    }];
    [request starWithFinishedBlock:^(NSError *error, id result) {
        
        if (!error) {
            self.sourceData=[AlbumRecmd mj_objectArrayWithKeyValuesArray:result[@"plaze_album_list"][@"RM"][@"album_list"][@"list"]];
            [self.albumCollectionView reloadData];
        }
    }];
    
    // Do any additional setup after loading the view.
}

- (NSArray *)sourceData {

    if (!_sourceData) {
        _sourceData = [[NSArray alloc] init];
    }
    return _sourceData;
}

- (UICollectionView *)albumCollectionView{
    
    if (!_albumCollectionView) {
        
        UICollectionViewFlowLayout *flowlayOut = [[UICollectionViewFlowLayout alloc] init];
        flowlayOut.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        flowlayOut.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowlayOut.minimumLineSpacing = 10; //行间距
        flowlayOut.minimumInteritemSpacing = 10;//列间距
        
        CGFloat width= (self.view.width - flowlayOut.sectionInset.left - flowlayOut.sectionInset.right -flowlayOut.minimumInteritemSpacing * 2)/3;
        flowlayOut.itemSize = CGSizeMake(width, 111);
        
        _albumCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowlayOut];
        _albumCollectionView.dataSource = self;
        _albumCollectionView.delegate = self;
        _albumCollectionView.backgroundColor = [UIColor whiteColor];
        [_albumCollectionView registerClass:[GeDanCollectionViewCell class] forCellWithReuseIdentifier:kNewstCollectionViewCellIdentifier];
    }
    return _albumCollectionView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UICollectionViewDataSource Methods

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.sourceData count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
        GeDanCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:kNewstCollectionViewCellIdentifier forIndexPath:indexPath];
        
        AlbumRecmd *item = self.sourceData[indexPath.row];
        
        cell.titleLabel.text = item.title;
        [cell.focusImageView sd_setImageWithURL:[NSURL URLWithString:item.pic_small] placeholderImage:nil];
   
    return cell;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
